home *** CD-ROM | disk | FTP | other *** search
Wrap
RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) NNNNaaaammmmeeee RWTValHashDictionary<K,V> - Rogue Wave library class SSSSyyyynnnnooooppppssssiiiissss #include <rw/tvhdict.h> unsigned hashFun(const K&); RWTValHashDictionary<K,V> dictionary(hashFun); PPPPlllleeeeaaaasssseeee NNNNooootttteeee!!!! IIIIffff yyyyoooouuuu ddddoooo nnnnooootttt hhhhaaaavvvveeee tttthhhheeee SSSSttttaaaannnnddddaaaarrrrdddd CCCC++++++++ LLLLiiiibbbbrrrraaaarrrryyyy,,,, uuuusssseeee tttthhhheeee iiiinnnntttteeeerrrrffffaaaacccceeee ddddeeeessssccccrrrriiiibbbbeeeedddd hhhheeeerrrreeee.... OOOOtttthhhheeeerrrrwwwwiiiisssseeee,,,, uuuusssseeee tttthhhheeee iiiinnnntttteeeerrrrffffaaaacccceeee ttttoooo RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhMMMMaaaapppp described in the Class Reference. DDDDeeeessssccccrrrriiiippppttttiiiioooonnnn RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy<<<<KKKK,,,,VVVV>>>> is a dictionary of keys of type KKKK and values of type VVVV, implemented using a hash table. While duplicates of values are allowed, duplicates of keys are not. It is a vvvvaaaalllluuuueeee based collection: keys and values are copied in and out of the hash buckets. Parameters KKKK and VVVV represent the type of the key and the type of the value, respectively, to be inserted into the table. These can be either classes or fundamental types. Classes KKKK and VVVV must have: well-defined copy semantics (TTTT::::::::TTTT((((ccccoooonnnnsssstttt TTTT&&&&)))) or equivalent); well-defined assignment semantics (TTTT::::::::ooooppppeeeerrrraaaattttoooorrrr====((((ccccoooonnnnsssstttt TTTT&&&&)))) or equivalent). In addition, class KKKK must have well-defined equality semantics (KKKK::::::::ooooppppeeeerrrraaaattttoooorrrr========((((ccccoooonnnnsssstttt KKKK&&&&)))))..in -5 A user-supplied hashing function for type KKKK must be supplied to the constructor when creating a new table. If KKKK is a Rogue Wave class, then this requirement is usually trivial because most Rogue Wave objects know how to return a hashing value. In fact, classes RRRRWWWWCCCCSSSSttttrrrriiiinnnngggg, RRRRWWWWDDDDaaaatttteeee, RRRRWWWWTTTTiiiimmmmeeee, and RRRRWWWWWWWWSSSSttttrrrriiiinnnngggg contain static member functions called hhhhaaaasssshhhh that can be supplied to the constructor as is. The function must have prototype: unsigned hhhhFFFFuuuunnnn(const K& a); and should return a suitable hash value for the object aaaa. To find a value, the key is first hashed to determine in which bucket the key and value can be found. The bucket is then searched for an object that is equal (as determined by the equality operator) to the key. The initial number of buckets in the table is set by the constructor. There is a PPPPaaaaggggeeee 1111 RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) default value. If the number of (key/value) pairs in the collection greatly exceeds the number of buckets then efficiency will sag because each bucket must be searched linearly. The number of buckets can be changed by calling member function rrrreeeessssiiiizzzzeeee(((()))). This is an expensive proposition because not only must all the items be copied into the new buckets, but all of the keys must be rehashed. If you wish this to be done automatically, then you can subclass from this class and implement your own special iiiinnnnsssseeeerrrrtttt(((()))) and rrrreeeemmmmoooovvvveeee(((()))) functions which perform a rrrreeeessssiiiizzzzeeee(((()))) as necessary. PPPPeeeerrrrssssiiiisssstttteeeennnncccceeee None EEEExxxxaaaammmmpppplllleeee #include <rw/tvhdict.h> #include <rw/cstring.h> #include <rw/rwdate.h> #include <rw/rstream.h> main() { RWTValHashDictionary<RWCString, RWDate> birthdays(RWCString::hash); birthdays.insertKeyAndValue( "John", RWDate(12, "April", 1975) ); birthdays.insertKeyAndValue("Ivan", RWDate(2, "Nov", 1980)); // Alternative syntax: birthdays["Susan"] = RWDate(30, "June", 1955); birthdays["Gene"] = RWDate(5, "Jan", 1981); // Print a birthday: cout << birthdays["John"] << endl; return 0; } Program output: April 12, 1975 PPPPuuuubbbblllliiiicccc CCCCoooonnnnssssttttrrrruuuuccccttttoooorrrrssss RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy<K,V>(unsigned (*hashKey)(const K&), size_t buckets = RWDEFAULT_CAPACITY); Constructs a new hash dictionary. The first argument is a pointer to a user-defined hashing function for items of type KKKK (the key). The table will initally have bbbbuuuucccckkkkeeeettttssss buckets although this can be changed with PPPPaaaaggggeeee 2222 RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) member function rrrreeeessssiiiizzzzeeee(((()))). RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy<K,V>(const RWTValHashDictionary<K,V>& dict); Copy constructor. Constructs a new hash dictionary as a copy of ddddiiiicccctttt. The new dictionary will have the same number of buckets as the old table. Hence, although the keys and values must be copied into the new table, the keys will not be rehashed. PPPPuuuubbbblllliiiicccc OOOOppppeeeerrrraaaattttoooorrrrssss RWTValHashDictionary<K,V>& ooooppppeeeerrrraaaattttoooorrrr====(const RWTValHashDictionary<K,V>& dict); Sets self to a copy of ddddiiiicccctttt. Afterwards, the new table will have the same number of buckets as the old table. Hence, although the keys and values must be copied into the new table, the keys will not be rehashed. V& ooooppppeeeerrrraaaattttoooorrrr[[[[]]]](const K& key); Look up the key kkkkeeeeyyyy and return its associated value as an llllvvvvaaaalllluuuueeee reference. If the key is not in the dictionary, then it is added to the dictionary. In this case, the value associated with the key will be provided by the default constructor for objects of type VVVV. PPPPuuuubbbblllliiiicccc MMMMeeeemmmmbbbbeeeerrrr FFFFuuuunnnnccccttttiiiioooonnnnssss void aaaappppppppllllyyyyTTTTooooKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee(void (*applyFun)(const K&, V&,void*), void* d); Applies the user-defined function pointed to by aaaappppppppllllyyyyFFFFuuuunnnn to every key- value pair in the dictionary. This function must have prototype: void yyyyoooouuuurrrrFFFFuuuunnnn(const K& key, V& value, void* d); The key will be passed by constant reference and hence cannot be changed. The value will be passed by reference and can be modified. Client data may be passed through as parameter dddd. void cccclllleeeeaaaarrrr(); PPPPaaaaggggeeee 3333 RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) Removes all items from the collection. RWBoolean ccccoooonnnnttttaaaaiiiinnnnssss(const K& key) const; Returns TTTTRRRRUUUUEEEE if the dictionary contains a key which is equal to kkkkeeeeyyyy. Returns FFFFAAAALLLLSSSSEEEE otherwise. Equality is measured by the class-defined equality operator for class KKKK. size_t eeeennnnttttrrrriiiieeeessss() const; Returns the number of key-value pairs currently in the dictionary. RWBoolean ffffiiiinnnndddd(const K& target, K& retKey) const; Returns TTTTRRRRUUUUEEEE if the dictionary contains a key which is equal to ttttaaaarrrrggggeeeetttt and puts the matching kkkkeeeeyyyy into rrrreeeettttKKKKeeeeyyyy. Returns FFFFAAAALLLLSSSSEEEE otherwise and leaves rrrreeeettttKKKKeeeeyyyy untouched. Equality is measured by the class-defined equality operator for class KKKK. RWBoolean ffffiiiinnnnddddVVVVaaaalllluuuueeee(const K& key, V& retVal) const; Returns TTTTRRRRUUUUEEEE if the dictionary contains a key which is equal to kkkkeeeeyyyy and puts the associated vvvvaaaalllluuuueeee into rrrreeeettttVVVVaaaallll. Returns FFFFAAAALLLLSSSSEEEE otherwise and leaves rrrreeeettttVVVVaaaallll untouched. Equality is measured by the class-defined equality operator for class KKKK. RWBoolean ffffiiiinnnnddddKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee(const K& key, K& retKey,V& retVal) const; Returns TTTTRRRRUUUUEEEE if the dictionary contains a key which is equal to kkkkeeeeyyyy and puts the matching kkkkeeeeyyyy into rrrreeeettttKKKKeeeeyyyy and the associated vvvvaaaalllluuuueeee into rrrreeeettttVVVVaaaallll. Returns FFFFAAAALLLLSSSSEEEE otherwise and leaves rrrreeeettttKKKKeeeeyyyy and rrrreeeettttVVVVaaaallll untouched. Equality is measured by the class-defined equality operator for class KKKK. void iiiinnnnsssseeeerrrrttttKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee(const K& key, const V& value); Inserts the key kkkkeeeeyyyy and value vvvvaaaalllluuuueeee into the dictionary. RWBoolean iiiissssEEEEmmmmppppttttyyyy() const; PPPPaaaaggggeeee 4444 RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++)))) Returns TTTTRRRRUUUUEEEE if the dictionary has no items in it, FFFFAAAALLLLSSSSEEEE otherwise. RWBoolean rrrreeeemmmmoooovvvveeee(const K& key); Returns TTTTRRRRUUUUEEEE and removes the (key/value) pair where the key is equal to the kkkkeeeeyyyy. Returns FFFFAAAALLLLSSSSEEEE if there is no such key. Equality is measured by the class-defined equality operator for class KKKK. void rrrreeeessssiiiizzzzeeee(size_t N); Changes the number of buckets to NNNN, a relatively expensive operation if there are many items in the collection. PPPPaaaaggggeeee 5555